﻿#include "redis_model.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <hiredis/hiredis.h>
#include <time.h>  

#define IP_MAX_LEN 16
#define REDIS_MAX_IP_CNT 8

#define IP_EXPIRE_TIME 1
#define SET_TEST_KEY "SECURITY_REDIS_TEST"
#define SET_TEST_VALUE "OK"
#define EXPIRATION_TIME 60*60
typedef struct
{
	int port;
	char ip[IP_MAX_LEN];
}ipdata;

int ipcount = 0;
ipdata ipbuf[REDIS_MAX_IP_CNT];

//static int analysis_tmp_black_list(redisReply *reply,tmp_black_ip_array *tmp_black_list);
//static int analysis_get_data(redisReply *reply,tmp_black_ip_array *tmp_black_list);
//static int find_data(char* dest);
//static int loop_strcpy_array(	redisReply *reply,str_array* data);

OUT_PUT_FUNC void set_redis_addr(IN_PUT huidu_address_info *addr, IN_PUT int count)
{
	printf("set_redis_addr() start\n");
	if(NULL == addr)return ;
	int index = 0;
	memset(ipbuf, 0, REDIS_MAX_IP_CNT*sizeof(ipdata));
	ipcount = count;
	for(index = 0;index < count;index++)
	{
		if(addr[index].ip.len > 0){
			memcpy(ipbuf[index].ip,addr[index].ip.data,addr[index].ip.len);
			ipbuf[index].ip[addr[index].ip.len+1]='\0';
			ipbuf[index].port = addr[index].port;
		}
	}
    return;
}

OUT_PUT_FUNC void * connet_to_redis(){
	printf("connet_to_redisex() start\n");

	redisContext *redis_c = NULL;
	int index = 0;
	ipdata ipdata_tmp;

    struct timeval timeout = { REDIS_CON_TIME_OUT_SEC, REDIS_CON_TIME_OUT_SEC*1000 }; 
	for(index = 0;index < ipcount;index++){
	   redis_c = redisConnectWithTimeout(ipbuf[index].ip, ipbuf[index].port, timeout);
	    if (redis_c == NULL || redis_c->err != 0) {
	        if (redis_c) {
	            printf("connet_to_redisex Connection : failed%s\n", redis_c->errstr);
	            redisFree(redis_c);
	        } else {
	            printf("connet_to_redisex Connection failed: can't allocate redis context\n");
	        }
			continue;
		}else{
			redisReply* reply = redisCommand(redis_c,"SET %s %s",SET_TEST_KEY,SET_TEST_VALUE);
			printf("set_ip_tempvisit_num() SET 111replytemp->str : %s\n", reply->str);
			if(reply->str && strcmp("OK",reply->str) == 0){

				if(index != 0)
				{
			    	memcpy(&ipdata_tmp,&(ipbuf[0]),sizeof(ipdata));
					memcpy(&(ipbuf[0]),&(ipbuf[index]),sizeof(ipdata));
					memcpy(&(ipbuf[index]),&ipdata_tmp,sizeof(ipdata));
				}
				freeReplyObject(reply); 
				break;
			}
			freeReplyObject(reply); 

		}
	}
	if(index == ipcount)
		redis_c = NULL;
    return redis_c;
}


OUT_PUT_FUNC void  free_connettion(IN_PUT void * redis_con){
	if(NULL == redis_con) return ;
	 redisFree((redisContext*)redis_con);
}

static void  free_str_array(str_array *arry){
	unsigned int i;

	if(NULL == arry) return;
	for(i =0; i <arry->size; i ++){
		free(arry->data[i].data);
		arry->data[i].data = NULL;
              arry->data[i].len = 0;
	}
	free(arry->data);
	arry->data = NULL;
	arry->size = 0;
}

OUT_PUT_FUNC void  free_huidu_info(huidu_info *info){

	if(NULL == info) return;
	free_str_array(&(info->ip_list));
	free_str_array(&(info->user_id_list));
	free_str_array(&(info->appid_list));
	free_str_array(&(info->bbversion_list));
}

static int loop_strcpy_array(redisReply *reply,str_array* data)
{

	int num = 0;
	int tnum = 0;
	if(NULL == reply || 0 >= reply->elements || NULL == data)return num;

	data->data = (huidu_model_str*)malloc(sizeof(huidu_model_str)*reply->elements);
	data->size = reply->elements;	

	
	for(num = 0;num < (int)data->size;num++){
		tnum = strlen(reply->element[num]->str);
		data->data[num].data = (unsigned char*)malloc(sizeof(char)*tnum);
		data->data[num].len = tnum;
		
		if(NULL != data->data[num].data){
			strncpy((char*)data->data[num].data,reply->element[num]->str,tnum);
		}

	}
	return num;
}


OUT_PUT_FUNC int  get_huidu_info(IN_PUT void *redis_con, OUT_PUT huidu_info * info)
{
	
	printf("get_huidu_info -----------\n");
	if(!redis_con || !info)return  -1;
	redisReply *reply = NULL;
	redisContext* redis_c = (redisContext* )redis_con;

	reply = redisCommand(redis_c,"SMEMBERS huidu:ip");
	printf("get_huidu_info() 11 SMEMBERS huidu:ip elements: %lu reply->element = %p\n", reply->elements,reply->element);
	if(NULL != reply->element && reply->element[0] != NULL && reply->element[0]->str != NULL){
		loop_strcpy_array(reply,&info->ip_list);
	}
	freeReplyObject(reply); 	
	
	reply = redisCommand(redis_c,"SMEMBERS huidu:userid");
	printf("get_huidu_info() 22 SMEMBERS huidu:userid elements: %lu\n", reply->elements);
	if(NULL != reply->element && reply->element[0] != NULL && reply->element[0]->str != NULL){
		loop_strcpy_array(reply,&info->user_id_list);
	}
	freeReplyObject(reply);

	reply = redisCommand(redis_c,"SMEMBERS huidu:appid");
	printf("get_huidu_info() 22 SMEMBERS huidu:appid elements: %lu\n", reply->elements);
	if(NULL != reply->element && reply->element[0] != NULL && reply->element[0]->str != NULL){
		loop_strcpy_array(reply,&info->appid_list);
	}
	freeReplyObject(reply);

	reply = redisCommand(redis_c,"SMEMBERS huidu:bbversion");
		printf("get_huidu_info() 22 SMEMBERS huidu:bbversion elements: %lu\n", reply->elements);
		if(NULL != reply->element && reply->element[0] != NULL && reply->element[0]->str != NULL){
			loop_strcpy_array(reply,&info->bbversion_list);
		}
	freeReplyObject(reply);
	/*
	if(nflag <= 0){
		reply = redisCommand(redis_c,"SET configempty 1");
		freeReplyObject(reply);		
	}	
	*/
	return 0;
}
#if 0
void Test()
{

	huidu_info info ={0};
	ip_fre_info ip_info = {0};
	ip_info.ip.data = (unsigned char*)malloc(sizeof(char)*32);
	ip_info.ip.len = 11;
	strcpy(ip_info.ip.data,"10.2.13.161");
	tmp_black_ip_array tmp_black_list = {0};
	struct timeval tms;
	char tstr[100];
	timerclear(&tms);
	gettimeofday(&tms, NULL);
	strftime(tstr, 100, "%X", localtime(&tms.tv_sec));
	// tv_usec?????????1000???????? 
	printf(" start %s.%ld\n", tstr, tms.tv_usec/1000);

	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
	printf("main ------1-----\n");
    redisContext* redis_c = redisConnectWithTimeout("127.0.1.1", 6379, timeout);
	printf("main ------2-----\n");
    if (redis_c == NULL || redis_c->err != 0) {
		printf("main ------3-----\n");
		if (redis_c) {
            printf("Connection error: %s\n", redis_c->errstr);
            redisFree(redis_c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
		return;
    }

	
	//printf("main -----------\n");


	//incr_ip_temp_view_count((void*)redis_c,ip_info.ip);
	//incr_ip_view_count((void*)redis_c,ip_info.ip,ip_info.ip);
	while(1){
	get_huidu_info((void*)redis_c,&info);
	free_huidu_info(&info);
		get_tmp_black_list((void*)redis_c,&tmp_black_list);
		printf("Test ========\n");
		free_tmp_black_ip_array(&tmp_black_list);
		sleep(1);
	}
	timerclear(&tms);
	gettimeofday(&tms, NULL);
	strftime(tstr, 100, "%X", localtime(&tms.tv_sec));
	//tv_usec?????????1000???????? 
	printf(" start %s.%ld\n", tstr, tms.tv_usec/1000);

	huidu_address_info ip[3]= {
		{{10,"10.2.50.36"},6379},
		{{9,"127.0.0.1"},6379},
		{{10,"10.2.50.38"},6379}
			};
	int port[6]= {2464,6379,254,4656.,65443,2045};
	//set_redis_addr(ip,3);
	//redisContext* redis_c = connet_to_redis();
	//printf("Main redis_c=%p",redis_c);

	if(ip_info.ip.data)free(ip_info.ip.data);
	ip_info.ip.data = NULL;
	return;
}
int main(){	
	//while(1){
		Test();
		//sleep(1);
	//}
	return 0;
}
#endif
